package org.chen.web.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.chen.model.service.BaseService;
import org.chen.utils.ResultJson;
import org.chen.utils.ResultPageJson;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

public class BaseController<T> {

	@Autowired
	private BaseService<T> baseService;

	@ResponseBody
	@RequestMapping(value = "/page/{pageSize}/{currentPage}", method = RequestMethod.GET)
	public ResultPageJson queryByPage(@PathVariable Long pageSize, @PathVariable Long currentPage) {

		ResultPageJson json = new ResultPageJson();
		// 总行数
		Long rowCount = baseService.queryRowCount();
		// 总页数
		Long pageCount = (rowCount - 1) / pageSize + 1;

		if (currentPage > pageCount) {
			currentPage = pageCount;
		}
		if (currentPage < 1) {
			currentPage = 0L;
		}
		if (currentPage > 0) {
			currentPage -= 1;
		}

		// 起始行
		Long startIndex = pageSize * currentPage;
		Map<String, Object> pageMap = new HashMap<String, Object>();
		pageMap.put("startIndex", startIndex);
		pageMap.put("pageSize", pageSize);

		List<T> list = baseService.queryByPage(pageMap);
		if (list != null && list.size() > 0) {
			json.setMsgcode(200);
			json.setMsginfo("success");
			json.setResult(list);
			json.setPageCount(pageCount);
		}
		return json;
	}

	@ResponseBody
	@RequestMapping(value = "{id}", method = RequestMethod.GET)
	public ResultJson queryById(@PathVariable Integer id) {
		ResultJson json = new ResultJson();
		T t = baseService.queryById(id);
		if (t != null) {
			json.setMsgcode(200);
			json.setMsginfo("success");
			json.setResult(t);
		}
		return json;
	}

	@ResponseBody
	@RequestMapping(value = "", method = RequestMethod.GET)
	public ResultJson query() {
		ResultJson json = new ResultJson();
		List<T> list = baseService.query();
		if (list != null && list.size() > 0) {
			json.setMsgcode(200);
			json.setMsginfo("success");
			json.setResult(list);
		}
		return json;
	}

	@ResponseBody
	@RequestMapping(value = "{id}", method = RequestMethod.PUT)
	public ResultJson update(@PathVariable Integer id, @RequestBody T t) {
		ResultJson json = new ResultJson();
		T model = baseService.queryById(id);
		if (model != null) {
			model = t;
			Integer row = baseService.update(model);
			if (row > 0) {
				json.setMsgcode(200);
				json.setMsginfo("success");
			}
		}
		return json;
	}

	@ResponseBody
	@RequestMapping(value = "{id}", method = RequestMethod.DELETE)
	public ResultJson delete(@PathVariable Integer id) {
		ResultJson json = new ResultJson();
		Integer row = baseService.delete(id);
		if (row > 0) {
			json.setMsgcode(200);
			json.setMsginfo("success");
		}
		return json;
	}

	@ResponseBody
	@RequestMapping(value = "", method = RequestMethod.POST)
	public ResultJson add(@RequestBody T t) {
		ResultJson json = new ResultJson();
		Integer row = baseService.add(t);
		if (row > 0) {
			json.setMsgcode(200);
			json.setMsginfo("success");
		}
		return json;
	}

}
